package com.ApiBack.springbootinit.client;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONUtil;
import com.ApiBack.springbootinit.model.entity.Location;
import com.ApiBack.springbootinit.model.entity.UserTest;
import com.ApiBack.springbootinit.utils.Encrypt;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class CLient {

    private String accessKey;
    private String secretKey;
    private static final String BODY="Igrowing-API";

    public CLient(String accessKey, String secretKey) {
        this.accessKey = accessKey;
        this.secretKey = secretKey;
    }

    public CLient() {
    }






    public String invokeApi(UserTest user,String url) {
        String userStr = JSONUtil.toJsonStr(user);
        HttpResponse httpResponse = HttpRequest.post(url)
                .addHeaders(getHeader(userStr))
                .body(userStr)
                .execute();
        String body = httpResponse.body();
        return body;
    }

    public String getPoisonousChickenSoup(String url)
    {
        HttpResponse httpResponse = HttpRequest.get(url)
                .addHeaders(getHeader(BODY))
                .body("")
                .execute();
        return httpResponse.body();
    }

    public String getLocalWeather(String url)
    {
        HttpResponse httpResponse = HttpRequest.get(url)
                .addHeaders(getHeader(BODY))
                .body("")
                .execute();
        return httpResponse.body();
    }


    public String getWeatherByLocation(String url, Location location)
    {
        String locationStr = JSONUtil.toJsonStr(location);
        HttpResponse httpResponse = HttpRequest.post(url)
                .addHeaders(getHeader(locationStr))
                .body(locationStr)
                .execute();
        return httpResponse.body();
    }

    public String getJoke(String url)
    {
        HttpResponse httpResponse = HttpRequest.get(url)
                .addHeaders(getHeader(BODY))
                .body("")
                .execute();
        return httpResponse.body();
    }


    public String getEnglish(String url)
    {
        HttpResponse httpResponse = HttpRequest.get(url)
                .addHeaders(getHeader(BODY))
                .body("")
                .execute();
        return httpResponse.body();
    }

    public String getWallpaper(String url)
    {
        HttpResponse httpResponse = HttpRequest.get(url)
                .addHeaders(getHeader(BODY))
                .body("")
                .execute();
        return httpResponse.body();
    }

    private Map<String, String> getHeader(String body) {
        HashMap<String, String> HeaderHashMap = new HashMap<>();

        HeaderHashMap.put("accessKey", accessKey);
        HeaderHashMap.put("body", body);
        HeaderHashMap.put("Key", Encrypt.encrypt(body, secretKey));
        HeaderHashMap.put("nonce", RandomUtil.randomNumbers(12));
        HeaderHashMap.put("timestamp", String.valueOf(System.currentTimeMillis()/1000));
        return HeaderHashMap;

    }


}